package cn.usian.basicInformation.log.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.usian.basicInformation.log.mapper.LogMapper;
import cn.usian.basicInformation.log.service.ILogService;
import cn.usian.dto.basicInformation.log.BaseOperationLogDto;
import cn.usian.pojo.basicInformation.log.BaseOperationLog;
import cn.usian.pojo.basicInformation.log.LogDto;
import cn.usian.vo.basicInformation.log.BaseOperationLogVo;
import cn.usian.vo.basicInformation.log.LogVo;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
@Slf4j
public class LogServiceImpl implements ILogService {
    @Autowired
    private LogMapper logMapper;

    @Override
    public PageInfo<BaseOperationLogVo> list(BaseOperationLogDto logDto) {
        log.info("logDto:{}", logDto);
        LogDto log = new LogDto();
        BeanUtil.copyProperties(logDto, log);
        PageHelper.startPage(logDto.getPageNum(), logDto.getPageSize());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (logDto.getStartTime() != null && !logDto.getStartTime().isEmpty()) {
            log.setStartTime(LocalDateTime.parse(logDto.getStartTime(), formatter));
        } else {
            // 如果没有指定开始时间，则设置为null
            log.setStartTime(null);
        }

        if (logDto.getEndTime() != null && !logDto.getEndTime().isEmpty()) {  // 修改这里
            log.setEndTime(LocalDateTime.parse(logDto.getEndTime(), formatter));
        } else {
            // 如果没有指定结束时间，则设置为null
            log.setEndTime(null);
        }
        String[] split = logDto.getOperationType().split(",");
        log.setOperationType(Arrays.asList(split));
        if("0".equals(logDto.getOperationModule())){
            log.setOperationModule(null);
        }
        List<BaseOperationLogVo> list = logMapper.list(log);
        PageInfo<BaseOperationLogVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void save(BaseOperationLog logInfo) {
        if(logInfo != null){
            logMapper.save(logInfo);
        }
    }
}
